package com.start.chat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.chat.entity.StartImGroupMsgEntity;
import com.start.chat.entity.StartImMsgEntity;
import com.start.chat.handler.StartChatGroupMsgHandler;
import com.start.chat.handler.StartChatSingleMsgHandler;
import com.start.chat.service.StartImGroupMsgService;
import com.start.chat.service.StartImSingleMsgService;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.im.core.model.StartBaseMessage;
import com.start.security.StartTokenTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/startImMsg")
public class StartMsgController {

    @Autowired
    private StartImSingleMsgService singleMsgService;
    @Autowired
    private StartImGroupMsgService groupMsgService;
    @Autowired
    private StartChatSingleMsgHandler singleMsgHandler;
    @Autowired
    private StartChatGroupMsgHandler groupMsgHandler;

    @GetMapping("/listNewestMsgList")
    public Result<?> listNewestMsgList() {
        String userId = StartTokenTools.getCurrentUser().getUserId();

        return Result.ok();
    }

    @GetMapping("/pageSingleMsg")
    public Result<?> pageSingleMsg(Page<StartImMsgEntity> page) {
        LambdaQueryWrapper<StartImMsgEntity> queryWrapper = DataQueryConstructor.createEntity(StartImMsgEntity.class).lambda();
        queryWrapper.orderByDesc(StartBaseMessage::getSendTime);
        return Result.ok(singleMsgService.page(page, queryWrapper));
    }



    @GetMapping("/pageGroupMsg")
    public Result<?> pageGroupMsg(Page<StartImGroupMsgEntity> page) {
        LambdaQueryWrapper<StartImGroupMsgEntity> queryWrapper = DataQueryConstructor.createEntity(StartImGroupMsgEntity.class).lambda();
        queryWrapper.orderByDesc(StartBaseMessage::getSendTime);
        return Result.ok(groupMsgService.page(page, queryWrapper));
    }

    @PostMapping("/sendSingleMsg/{sendClientType}/{clientMsgId}")
    public Result<?> sendSingleMsg(@PathVariable("clientType") String sendClientType,
                                   @PathVariable("clientMsgId") String clientMsgId,
                                   @RequestBody StartImMsgEntity entity) {
        entity.setSendClientType(sendClientType);
        String userId = StartTokenTools.getCurrentUser().getUserId();
        entity.setSendUserId(userId);
        singleMsgHandler.sendMsg(entity);
        return Result.ok();
    }

    @PostMapping("/sendGroupMsg/{sendClientType}/{clientMsgId}")
    public Result<?> sendGroupMsg(@PathVariable("sendClientType") String sendClientType,
                                  @PathVariable("clientMsgId") String clientMsgId,
                                  @RequestBody StartImGroupMsgEntity entity) {
        entity.setSendClientType(sendClientType);
        String userId = StartTokenTools.getCurrentUser().getUserId();
        entity.setSendUserId(userId);
        groupMsgHandler.sendMsg(entity);
        return Result.ok();
    }

}
